﻿#ifndef IM_Account_h__
#define IM_Account_h__

#include <string>
#include <map>
#include <vector>
#include <mutex>
#include "Log.h"
#include "Utility.h"
#include "IM_DataInterface.h"
#include <mutex>

class IM_FriendMap;
class IM_Agent;
class IM_AccountServer;


enum AccountState
{
	off_line,on_line
};

class IM_Account
{
public:
	void print_infor();
	void print_friends_infor();
	IM_Account(const std::string& ac_number, const std::string & name, const std::string & emil, char sex, IM_DataInterface * data_interface);
	IM_Account(std::string&& ac_number, std::string && name, std::string && emil, char sex, IM_DataInterface * data_interface);

	AccountState get_account_state() { std::lock_guard<std::mutex>{m_mutex_state}; return m_curr_state; }
	void set_account_state(AccountState state);
	
	IM_FriendMap * get_friend_map();

	const std::string&  get_name();
	const std::string&  get_emil();
	char				get_sex();

private:
	IM_Account() = delete;
	~IM_Account();

	IM_Account & operator=(const IM_Account &) = delete;
	IM_Account & operator=(IM_Account &&) = delete;

	friend class IM_AccountServer;

	//状态发生改变
	void _state_change();  

	void _init_friend_map();
	void _relace_friend_map();
private:
	//账号 
	std::string   m_account_number;
	//好友映射
	IM_FriendMap  * m_friend_map;
	//当前状态
	AccountState  m_curr_state;
	//对应的服务代理 
	IM_Agent      *m_agent;
	//昵称
	std::string   m_name;
	//邮箱
	std::string   m_emil;
	//性别
	char          m_sex;
	//数据服务 
	IM_DataInterface  * m_data_interface;
	//state mutex
	std::mutex    m_mutex_state;
};

/******************************************/
//类名：IM_AccountServer
//创建时间 ：2018/5/20
//类型：singleton
//描述：describe
/******************************************/

class IM_AccountServer
{
private:
	IM_AccountServer();
	~IM_AccountServer();
	IM_AccountServer(const IM_AccountServer & o)=delete;
	IM_AccountServer(const IM_AccountServer && o)=delete;
	IM_AccountServer& operator=(const IM_AccountServer & o)=delete;
	
public:
	static IM_AccountServer * getInstance(){static IM_AccountServer c;return &c;}

public:
	//typedef std::function<const std::vector<std::string>& (std::string)> call_get_friends;
	bool init();
	//bool initAcFriendMap(call_get_friends get_friends_func);
	void clear();

	//************************************
	// 函数名:    findAccount
	// 创建日期： 2018/5/19
	// 使用权限:  public 
	// 参数:      const std::string & number
	// 返回值:    IM_Account *
	// 线程安全：  是
	//************************************
	IM_Account * findAccount(const std::string& number);
	//************************************
	// 函数名:    addAccount
	// 创建日期： 2018/5/19
	// 使用权限:  public 
	// 参数:      const std::string & number
	// 参数:      IM_Account * ac 
	// 参数:      bool overrides    如果number 对应的 ac 已经存在  是否允许覆盖  
	// 返回值:    bool 
	// 线程安全：  是
	//************************************
	bool  addAccount(const std::string &number, IM_Account* ac, bool overrides = false);
private:


private:
	IM_DataInterface  * m_data_interface;
	std::map<std::string, IM_Account *> m_ac_map;
	std::mutex   m_mutex;
	


};

#endif // IM_Account_h__


